<template>
	<view>
		<navbox title="家庭账单"></navbox>
		<view style="background-color: #f9f9f9;height: 1600rpx;">
			<view class="conn">
				<view style="text-align: center;font-weight: 600;color: #434343;margin-top: 50rpx;">———专属邀请码———</view>
				<view style="margin-top: 70rpx;margin-bottom: 50rpx;margin-left: 50rpx;margin-right: 50rpx;">
					<u--input border="bottom" v-model="code" inputAlign="center"></u--input>
				</view>

				<view style="margin-left: 160rpx;margin-right: 160rpx;">
					<u-button type="primary" text="获取邀请码" @click="getCode()"></u-button>
				</view>

				<view style="margin-top: 150rpx;margin-bottom: 40rpx;">
					<u-line color="#969494" dashed></u-line>
				</view>
				<u--text type="info" text="TA已经创建家庭账单，填写邀请码加入" size="12" align="center"></u--text>

				<view style="margin-top: 30rpx;margin-bottom: 20rpx;margin-left: 50rpx;margin-right: 50rpx;">
					<u--input border="bottom" clearable v-model="checkCode" i inputAlign="center"></u--input>
				</view>
				<view style="margin-left: 160rpx;margin-right: 160rpx;margin-top: 60rpx;">
					<u-button type="primary" text="填写邀请码" @click="checkCodes()"></u-button>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import navbox from '../components/navbox/navbox.vue'
	export default {
		data() {
			return {
				code: '',
				checkCode: '',
				user: uni.getStorageSync("user")
			}
		},
		onPullDownRefresh() {
            this.checkUser()
			uni.stopPullDownRefresh()
		},
		methods: {
			async getCode() {
				const res = await this.$myRuquest({
					url: '/wexinLogin/account/getCode?userId=' + this.user.id,
					method: 'post',
				})
				this.code = res.data.data
			},
			async checkCodes() {
				const res = await this.$myRuquest({
					url: '/wexinLogin/account/checkCode?userId=' + this.user.id + '&code=' + this.checkCode,
					method: 'post',
				})
				if (res.data.code === 200) {
					this.user.famId = res.data.data
					uni.setStorageSync("user", this.user)
					uni.navigateTo({
						url: '/pagesa/famAccount/famAccount'
					})
				}
				uni.showToast({
					title: res.data.msg,
					icon: 'none'
				})
			},
			async checkUser(){
				const res = await this.$myRuquest({
					url: '/wexinLogin/account/checkUser?userId=' + this.user.id,
					method: 'post',
				})
				if(res.data.data!==0){
					this.user.famId = res.data.data
					uni.setStorageSync("user", this.user)
					uni.navigateTo({
						url: '/pagesa/famAccount/famAccount'
					})
				}
			}
		},
		components: {
			"navbox": navbox
		},
	}
</script>

<style>
	.conn {
		background-color: #fff;
		height: 860rpx;
		position: absolute;
		top: 250rpx;
		left: 30rpx;
		right: 30rpx;
		border-radius: 20rpx;
		box-shadow: 0px 0px 1px rgba(0, 0, 0, 0.3);
	}
</style>
